import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const Comment = createSlice({
    name: "Comment",
    initialState:{
        list:[],
    },
    reducers:{
        setList:(state,action)=>{
            state.list = action.payload;
        },
        addList:(state,action)=>{
            state.list.push(action.payload)
        }
    }   
})

const { setList,addList } = Comment.actions;

const getList = ()=>{
    return (dispatch)=>{
        axios.get("http://localhost:3004/list").then(response=>{
            dispatch(setList(response.data));
        })
    }
}
const addData = (data)=>{
    return async (dispatch)=>{
        let res = await axios.post("http://localhost:3004/list",data)
        dispatch(addList(res.data))

    }
}
export {getList,addData}
export default Comment.reducer